package com.uwo.commons.configuration.aop.validate.validate;

import com.uwo.commons.configuration.aop.validate.ErrorCode;
import com.uwo.commons.configuration.aop.validate.ValidateField;
import com.uwo.commons.configuration.aop.validate.ValidateProperties;
import com.uwo.commons.configuration.aop.validate.annotation.NotEmpty;
import com.uwo.commons.configuration.aop.validate.excepteion.ApiValidateExceptrion;
import com.uwo.commons.kits.StringKits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * 判断是否为空
 * @author hao.yan
 */
@Component(value = "Validate_NotEmpty")
public class NotEmptyValidate extends NotNullValidate{

    @Autowired
    private ValidateProperties validateProperties;

    @Override
    public void validate(Annotation annotation, ValidateField validateField) throws ApiValidateExceptrion {
        super.validate(annotation, validateField);
        if (validateField.getValue() instanceof String) {
            if(StringKits.isEmpty(String.valueOf(validateField.getValue()))) {
                NotEmpty notEmpty = (NotEmpty)annotation;
                String message = value(validateProperties.getMessages(), notEmpty.message(),
                        ErrorCode.PARAM_EMPTY.getMessage(validateField.getName()));
                exception(notEmpty.code(), message, ErrorCode.PARAM_EMPTY);
            }
        }
    }

}
